﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Win32API
{
    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;

        public short dmOrientation;
        public short dmPaperSize;
        public short dmPaperLength;
        public short dmPaperWidth;

        public short dmScale;
        public short dmCopies;
        public short dmDefaultSource;
        public short dmPrintQuality;
        public short dmColor;
        public short dmDuplex;
        public short dmYResolution;
        public short dmTTOption;
        public short dmCollate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmFormName;
        public short dmLogPixels;
        public short dmBitsPerPel;
        public int dmPelsWidth;
        public int dmPelsHeight;

        public int dmDisplayFlags;
        public int dmDisplayFrequency;

        public int dmICMMethod;
        public int dmICMIntent;
        public int dmMediaType;
        public int dmDitherType;
        public int dmReserved1;
        public int dmReserved2;

        public int dmPanningWidth;
        public int dmPanningHeight;
    };

    public class User32
    {
        [DllImport("user32.dll")]
        public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
        [DllImport("user32.dll")]
        public static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);

        public const int ENUM_CURRENT_SETTINGS = -1;
        public const int CDS_UPDATEREGISTRY = 0x01;
        public const int CDS_TEST = 0x02;
        public const int DISP_CHANGE_SUCCESSFUL = 0;
        public const int DISP_CHANGE_RESTART = 1;
        public const int DISP_CHANGE_FAILED = -1;

        public static void EnumDisplaySettings(ref List<DEVMODE> devModes)
        {
            DEVMODE devMode = new DEVMODE();
            devMode.dmDeviceName = new String(new char[32]);
            devMode.dmFormName = new String(new char[32]);
            devMode.dmSize = (short)Marshal.SizeOf(devMode);
            //List<DEVMODE> devModes = new List<DEVMODE>();
            for (int i = 0; User32.EnumDisplaySettings(null, i, ref devMode) > 0; ++i)
            {
                if (devMode.dmBitsPerPel < 16 || devMode.dmPelsHeight < 480)
                    continue;

                devModes.Add(devMode);
                string strMode = devMode.dmPelsWidth.ToString() + " x " + devMode.dmPelsHeight.ToString();
                //if (!videoMode.possibleValues.Contains(strMode))
                //{
                //    videoMode.possibleValues.Add(strMode);
                //}
            }
        }
    }
}
